import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '@/store'

import HomeIndex from '@/views/Index'
import Order from '@/views/Order'
import Mine from '@/views/Mine'
import CategoryDetail from '@/views/category/CategoryDeatil'
import CommodityDetail from '@/views/commodity/CommodityDetail'

Vue.use(VueRouter)

const routes = [
  {
    path: '/test',
    name: 'test',
    component: () => import('@/views/test/Test'),
    children: [
      {
        path: 'testScroll',
        name: 'testScroll',
        component: () => import('@/views/test/TestScroll')
      }
    ]
  },
  {
    path: '/',
    name: 'home',
    component: HomeIndex
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: Order,
  },
  {
    path: '/mine',
    name: 'mine',
    component: Mine
  },
  {
    path: '/categoryDetail/:id',
    name: 'categoryDetail',
    component: CategoryDetail,
  },
  {
    path: '/collection/myCollection',
    name: 'myCollection',
    component: () => import('@/views/collection/myCollection'),
  },
  {
    path: '/commodityDetail/:id',
    name: 'commodityDetail',
    component: CommodityDetail,
  },
  {
    path: '/storeDetail/:id',
    name: 'storeDetail',
    component: () => import('@/views/store/StoreDetail'),
  },
  {
    path: '/comment',
    name: 'comment',
    component: () => import('@/views/comment/Comment'),
    children: [
      {
        path: 'myComment',
        name: 'myComment',

        component: () => import('@/views/comment/MyComment'),
      },
    ]
  },
  {
    path: '/addOrder',
    name: 'addOrder',
    component: () => import('@/components/order/AddOrder')
  },
  {
    path: '/address',
    name: 'address',
    component: () => import('@/views/address/Address'),
    children: [
      {
        path: 'myAddress',
        name: 'myAddress',

        component: () => import('@/views/address/MyAddress'),
      },
      {
        path: 'addressEdit',
        name: 'addressEdit',
        component: () => import('@/views/address/AddressEdit'),

      }
    ]
  },
  {
    path: '/coupon/list',
    name: 'couponList',
    component: () => import('@/views/coupon/CouponList')
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem('token')
  
  if(!token && to.name != 'login')
    next({name:'login'})


  let nameList = ['home', 'order', 'mine']
  //判断是否是首页、订单、我的路由，并且将值传到vuex
  let name = to.name
  let showTabbar = false

  for(let i = 0; i < nameList.length; i++)  
    if(nameList[i] == name) showTabbar =true

  store.commit('setShowTabbar', showTabbar)
  next()
})

export default router
